﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Collections;

public partial class CMS_Admin_PageSetting : System.Web.UI.Page
{
    Usas.BLL.Cms.cms_block bl = new Usas.BLL.Cms.cms_block();
    Usas.BLL.Cms.cms_block_setting bls = new Usas.BLL.Cms.cms_block_setting();

    Usas.Model.Cms.cms_block blm = new Usas.Model.Cms.cms_block();
    Usas.Model.Cms.cms_block_setting blsm = new Usas.Model.Cms.cms_block_setting();

    
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
        catch
        {
        }
    }

    private void Bind()
    {
        DataTable dt = bls.GetList("s_page='" + Request["page"].ToString() + "'").Tables[0];
        mainappend.Text += getplace(dt.Select("s_location like '1,%'", "s_location asc"), true);
        mainappend.Text += getplace(dt.Select("s_location like '2,%'", "s_location asc"), false);
    }

    private string getplace(DataRow[] dr, bool isleft)
    {

        StringBuilder sbr = new StringBuilder();
        if (isleft)
        {
            sbr.Append("<div class='dvleft'>");
            sbr.Append("<div>");
            sbr.Append("<a href='AddBlock.aspx?block=1&page=Default.aspx&keepThis=true&TB_iframe=true&height=80&width=400&' title='添加模块' class='thickbox'>添加模块</a>");
            //sbr.Append("<input type='button' value='添加模块' onclick=\"doit('1')\">");
            sbr.Append("</div>");
            sbr.Append("<div id='widget-place-1' class='widget-place column1'>");
        }
        else
        {
            sbr.Append("<div class='dvright'>");
            sbr.Append("<div>");
            sbr.Append("<a href='AddBlock.aspx?block=2&page=Default.aspx&keepThis=true&TB_iframe=true&height=80&width=400&' title='添加模块' class='thickbox'>添加模块</a>");

            sbr.Append("</div>");
            sbr.Append("<div id='widget-place-2' class='widget-place column2'>");
        }

        for (int i = 0; i < dr.Length; i++)
        {
            blm = bl.GetModel(Convert.ToInt32(dr[i]["s_b_id"]));

            string set = blm.b_setting_url == null || blm.b_setting_url == "" ? "" : "<a href='" + blm.b_setting_url + "?control=" + blm.b_id + "&page=" + Request["page"].ToString() + "&keepThis=true&TB_iframe=true&height=500&width=700' class='thickbox' title='" + blm.b_name + "'>设置</a>";
            sbr.Append("<div class='widget movable removable' id='" + blm.b_id + "'>");
            sbr.Append("<div class='widget-header'>");
            sbr.Append("<strong>" + blm.b_name + "</strong>" + set);
            sbr.Append("</div>");
            sbr.Append("<div class='widget-content'>");
            sbr.Append(blm.b_remark + "</div>");
            sbr.Append("</div>");
        }
        sbr.Append("</div>");
        sbr.Append("</div>");

        return sbr.ToString();
    }

    protected void save_Click(object sender, EventArgs e)
    {
        List<string> lsDel = new List<string>();
        List<string> str = new List<string>();

        if (Request.Cookies["ew-position"] != null && Request.Cookies["ew-position"].Value.Trim() != "")
        {

            string[] part = Server.UrlDecode(Request.Cookies["ew-position"].Value).Split('|');

            for (int i = 0; i < part.Length; i++)
            {
                string[] block = null;
                if (part[i].IndexOf("widget-place-1=") != -1)
                {
                    block = part[i].Replace("widget-place-1=", "").Trim() == "" ? null : part[i].Replace("widget-place-1=", "").Split(',');
                    if (block != null)
                    {
                        for (int j = 0; j < block.Length; j++)
                        {
                            if (bl.Exists(Convert.ToInt32(block[j])))
                                lsDel.Add("1," + Convert.ToString(j + 1) + "|" + block[j]);
                        }
                    }
                }
                else if (part[i].IndexOf("widget-place-2=") != -1)
                {
                    block = part[i].Replace("widget-place-2=", "").Trim() == "" ? null : part[i].Replace("widget-place-2=", "").Split(',');
                    if (block != null)
                    {
                        for (int j = 0; j < block.Length; j++)
                        {
                            if (bl.Exists(Convert.ToInt32(block[j])))
                                lsDel.Add("2," + Convert.ToString(j + 1) + "|" + block[j]);
                        }
                    }
                }
            }
            //if (lsDel != null && lsDel.Count > 0)
            //{
            //    if (bls.BlockSort(lsDel, "Default.aspx") > 0)
            //    {

            //    }
            //}
            //Deletecookie('ew-close');
        }
        if (Request.Cookies["ew-close"] != null && Request.Cookies["ew-close"].Value.Trim() != "")
        {
            string[] part = Server.UrlDecode(Request.Cookies["ew-close"].Value).Split(',');
            for (int i = 0; i < part.Length; i++)
            {
                str.Add(part[i]);
            }
        }

        if ((lsDel != null || lsDel.Count > 0) || (str != null && str.Count > 0))
        {
            if (bls.BlockSort(lsDel, str, Request["page"].ToString()) > 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('布局保存成功！');location.href='" + Request.Url.ToString() + "'", true);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
}
